<script setup>
import { onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'

const route = useRoute()
const userStore = useUserStore()

// 初始化用户信息
const initUserInfo = async () => {
  if (userStore.isLogin && userStore.needRefresh) {
    try {
      await userStore.refreshUserInfo()
    } catch (error) {
      console.error('初始化用户信息失败:', error)
    }
  }
}

// 监听路由变化，需要时刷新用户信息
watch(
  () => route.path,
  () => {
    if (userStore.isLogin && userStore.needRefresh) {
      initUserInfo()
    }
  }
)

onMounted(() => {
  initUserInfo()
})
</script>

<template>
  <router-view />
</template>

<style>
html {
  background-color: #f7f8fa;
}

body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica,
    Segoe UI, Arial, Roboto, 'PingFang SC', 'miui', 'Hiragino Sans GB', 'Microsoft Yahei',
    sans-serif;
}

#app {
  min-height: 100vh;
}
</style>
